package com.aaa.spring.service;

import com.aaa.spring.exception.BizException;
import com.aaa.spring.mapper.GoodsInfoMapper;
import com.aaa.spring.mapper.GoodsTypeMapper;
import com.aaa.spring.pojo.GoodsInfo;
import com.aaa.spring.pojo.GoodsType;
import com.aaa.spring.pojo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品服务
 */
@Service
public class GoodsInfoService {
    //声明日志变量
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    /**
     * 添加商品
     * @param goodsInfo
     * @return
     */
    public Result add(GoodsInfo goodsInfo) {
        Result result = new Result();
        result.setCode(200);//表示正常
        result.setMsg("添加成功");

        if(StringUtils.isEmpty(goodsInfo.getGoodsid())){
            throw new BizException(123,"商品编号不能为空!");
        }
        if(StringUtils.isEmpty(goodsInfo.getGoodsname())){
            throw new BizException(123,"商品名称不能为空!");
        }
        if(goodsInfo.getGoodstype() == null){
            throw new BizException(123,"商品类型不能为空!");
        }
        if(goodsInfo.getPrice() == null){
            throw new BizException(123,"商品价格不能为空!");
        }
        //入库
//        goodsInfo.setCreatetime(new Date());
        goodsInfoMapper.add(goodsInfo);
        return result;
    }

    public Result getTypeList() {
        List<GoodsType> list = goodsTypeMapper.getAll();


        Result result = new Result();
        result.setCode(200);//表示正常
        result.setMsg("查询类型成功");
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        result.setMap(map);
        return result;
    }

    public Result getGoodsInfoList() {
        List<GoodsInfo> list = goodsInfoMapper.getAll();

        Result result = new Result();
        result.setCode(200);//表示正常
        result.setMsg("查询成功");
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        result.setMap(map);
        return result;
    }
}
